<!--添加修改用户信息-->
<template>
    <a-modal v-model="showAddTable" :width="640"
             :title="title ? '组织机构新增' : '组织机构编辑'"
             :maskClosable="false"
             :destroyOnClose="true" @ok="saveApply"
             @cancel="cancelApply">
        <a-form :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
            <a-form-item v-bind="formStyle" label="机构名称" required>
                <a-input
                        placeholder="请输入机构名称"
                        :disabled="!title"
                        ref="refocus"
                        v-decorator="[
                        'name',
                        {
                            rules: [
                                { required: true, message: '请输入汉字、字母、数字、_或-字符' },
                                { validator: this.verify.validateName.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                />
            </a-form-item>
          <a-form-item label="机构类型："  v-bind="formStyle">
            <a-select placeholder="选择机构类型"
                      v-decorator="['type', { rules: [{ required: true, message: '选择机构类型' }] }]">
              <a-select-option v-for="item in orgTypeList" :value="Number(item.value)"
                               :key="item.value">{{
                  item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>

            <a-form-item v-bind="formStyle" label="上级机构" required>
                <a-tree-select
                        style="width: 100%"
                        :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                        :treeData="orgTree" placeholder="请选择上级机构"
                        v-decorator="['parentId', { rules: [{ required: true, message: '请选择上级机构' }] }]">
                    <span style="color: #08c" slot="title" slot-scope="{key, title}">{{title}}</span>
                </a-tree-select>
            </a-form-item>
            <a-form-item v-bind="formStyle" label="描述">
                <a-textarea v-decorator="['description',{   rules: [{validator:this.verify.validateDescripte.bind(this)}],
                               getValueFromEvent: (event) => {
                                   return this.verify.formalhundredSpace(event)
                               }
                            }]" :rows="3" placeholder="请输入描述" />
            </a-form-item>
        </a-form>
    </a-modal>
</template>

<script>
    import verify from 'libs/verify'
    import util from '@/libs/util.js'

    export default {
        name: 'addEdit',
        components: {
        },
        data () {
            return {
                form: this.$form.createForm (this),
                showAddTable: false,
                input: false,
              orgTypeList: [],
                verify,
                sysOrgForm: {
                    name: '',
                    parentId: '',
                    type: '',
                    description: ''
                },
                selectedNode: {},
                originalData: {}
            }
        },
        computed: {
            formStyle () {
                return this.$store.state.common.formStyle
            }
        },
        props: {
            sysOrgDTO: {
                type: Object,
                required: true
            },
            title: {
                type: Boolean,
                required: true
            },
            orgTree: {
                type: Array,
                required: true
            }
        },
        watch: {
            sysOrgDTO ( value ) {
                if (value.parentId === '' || value.parentId === null) {
                    value.parentId = this.orgTree[ 0 ].id
                    value.parentName = this.orgTree[ 0 ].title
                }
                this.selectedNode = {
                    id: value.parentId,
                    name: value.parentName
                }
            }
        },
        methods: {
          /**
           * Function    : loadPageDictDatas
           * Description : 加载数据字典
           * Input       : val
           * Return      : none
           */
          loadPageDictDatas () {
            // 操作类型
            this.$loadSysDicts (this.$dicts.sortCode.ORG_TYPE, this.orgTypeList)
          },
            /**
             * Function    : showModal
             * Description : 打开弹窗
             * Input       : none
             * Return      : none
             */
            showModal () {
                this.showAddTable = true
                this.$nextTick (() => {
                    this.form.setFieldsValue ({parentId: this.orgTree[ 0 ].id})
                })

            },
            /**
             * Function    : showEdit
             * Description : 编辑时显示
             * Input       : opt单个所有数据
             * Return      : none
             */
            showEdit ( opt ) {
                this.showAddTable = true
                this.id = opt.id
                this.$nextTick (() => {
                    this.form.setFieldsValue (this.$pick (opt, [ 'name', 'type','parentId', 'description' ]))
                })
            },

            /**
             * Function    : saveApply
             * Description : 用户信息新增、编辑保存
             * Input       : name
             * Return      : none
             */
            saveApply () {
                this.form.validateFields (( err, value ) => {
                    if (!err) {
                        if (this.title) {
                            /* let para = Object.assign({}, this.sysOrgDTO);*/
                            let para = Object.assign ({}, value)
                            this.$store.dispatch ('addSysOrgAct', para).then (response => {
                                this.showAddTable = false
                                if (response.code === 0) {
                                    this.$emit ('init')
                                    this.$message.success (response.message)
                                } else {
                                    this.$message.error (response.message)
                                }
                            })
                        } else {
                            if (this.sysOrgDTO.id === this.sysOrgDTO.parentId) {
                                this.$message.error ('上级机构不能是本身！！！')
                                return
                            }
                            if (this.originalData !== this.sysUserDtos) {
                                let para = Object.assign ({}, value)
                                para.id = this.id
                                this.$store.dispatch ('updSysOrgAct', para).then (response => {
                                    this.showAddTable = false
                                    if (response.code === 0) {
                                        this.$emit ('init')
                                        this.$message.success (response.message)
                                    } else {
                                        this.$message.error (response.message)
                                    }
                                })
                            } else {
                                this.showAddTable = false
                            }
                        }
                    }
                })
            },

            /**
             * Function    : cancelApply
             * Description : 关闭弹出框
             * Input       : name
             * Return      : none
             */
            cancelApply ( name ) {
                this.showAddTable = false
                let _this = this
                setTimeout (function () {
                    _this.form.resetFields ()
                }, 100)
            },

            /**
             * Function    : changeInputType
             * Description : 切换 输入框和select框
             * Input       : name
             * Return      : none
             */
            changeInputType ( name ) {
                this.input = !this.input
                let _this = this
                setTimeout (function () {
                    _this.form.resetFields ()
                }, 100)
            },

            /**
             * Function    : selectNodeId
             * Description :
             * Input       : val
             * Return      : none
             */
            selectNodeId ( val ) {
                let _this = this
                setTimeout (function () {
                    _this.form.resetFields ()
                }, 100)

                this.sysOrgDTO.parentId = val.id
                this.sysOrgDTO.parentName = val.name
            }
        },

        created () {
          this.loadPageDictDatas()
        }
    }
</script>
